package com.young.grpc.server;

import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import org.young.grpc.demo.ProductInfoGrpc;
import org.young.grpc.demo.ProductOuterClass;

/**
 * @description：
 * @author: yangyahui01
 * @date: 12/3/24 4:49 PM
 */
@Slf4j
public class ProductInfoImpl extends ProductInfoGrpc.ProductInfoImplBase {

    /**
     * @param request
     * @param responseObserver
     */
    @Override
    public void addProduct(ProductOuterClass.Product request, StreamObserver<ProductOuterClass.ProductId> responseObserver) {
        log.info("request:{}", request.toString());

        responseObserver.onNext(ProductOuterClass.ProductId.newBuilder().setValue(request.getId()).build()); // 方法响应
        responseObserver.onCompleted(); // 标记 RPC 调用完成
    }

    /**
     * @param request
     * @param responseObserver
     */
    @Override
    public void getProduct(ProductOuterClass.ProductId request, StreamObserver<ProductOuterClass.Product> responseObserver) {
        responseObserver.onNext(ProductOuterClass.Product.newBuilder().setId(request.getValue()).setName("test_grpc").build());
        responseObserver.onCompleted();
    }
}
